package com.zkas.munqr;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class DisplayMessageActivity extends Activity {
	
	public final static String PARAM_CUIT = "PARAM_CUIT";
	LocationManager locationManager;
	
	LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        	locationManager.removeUpdates(locationListener);
        	//goToUrl(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_message);
		goToUrl();
		//locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	    //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}
	
	public void goToUrl(/*Location location*/){
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		// ip 10.32.33.14 en la muni
	    String ip = sharedPref.getString(this.getString(R.string.preference_key_server), "");
	    String url = "http://" + ip + "/munqr/mqmainac.php";
	    String user = sharedPref.getString(this.getString(R.string.preference_key_username), "");
	    String password = sharedPref.getString(this.getString(R.string.preference_key_password), "");
	    Intent intent = getIntent();
	    String cuit = intent.getStringExtra(PARAM_CUIT);
	    //byte[] imageData = null;//intent.getByteArrayExtra(MainActivity.PARAM_IMAGE);
	    double latitude = 0; //location.getLatitude();
    	double longitude = 0; //location.getLongitude();
    	//String postData = bytesToHex(imageData);
		//String params = "image=" + postData;
		
		WebViewClient webViewClient = new WebViewClient();
		WebView webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.clearCache(true);
		webView.setWebViewClient(webViewClient);
		
		
		//webView.postUrl(url + "?cuit=" + cuit + "&user=" + user + "&password=" + password + "&lat=" + latitude + "&lon=" + longitude); //, EncodingUtils.getBytes(params, "base64"));
		webView.loadUrl(url + "?cuit=" + cuit + "&user=" + user + "&password=" + password + "&lat=" + latitude + "&lon=" + longitude );
	}

	public static String bytesToHex(byte[] bytes) {
	    if (bytes == null) {
			return "";
		}
	    
		final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	    char[] hexChars = new char[bytes.length * 2];
	    int v;
	    for ( int j = 0; j < bytes.length; j++ ) {
	        v = bytes[j] & 0xFF;
	        hexChars[j * 2] = hexArray[v >>> 4];
	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
	    }
	    return new String(hexChars);
	}
}
